package com.example.statistics.ui.yw

import android.os.Bundle
import android.view.View
import com.example.statistics.R
import com.example.statistics.bean.yw.ProcessingBean
import com.example.statistics.bean.yw.ProcessingMaterialBean
import com.example.statistics.bean.yw.ProcessingProductBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import kotlinx.android.synthetic.main.activity_pre_product_profit.*
import kotlinx.android.synthetic.main.layout_purchase_type_title.view.*
import kotlinx.android.synthetic.main.table_row_material.view.*
import java.math.BigDecimal

class PreProductProfitActivity : BaseActivity() {
    override val title: String
        get() = "预估"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pre_product_profit)
        val id = intent.getIntExtra(Constants.ID, 0)

        layout_material.tv_title.contentText = "原料"
        layout_material.iv_add.visibility = View.INVISIBLE
        layout_product.tv_title.contentText = "成品"
        layout_product.iv_add.visibility = View.INVISIBLE

        httpGet<ProcessingBean>(UrlConfig.PROCESSING_DATA,
            config = {
                isShowDialog = true
                listParams = listOf("ID" to id)
            }) {
            material(it)
            product(it?.productList)
        }

        table_material
        table_product
    }


    private fun material(processingBean: ProcessingBean?) {
        var materialFee = BigDecimal(0)
        table_material.removeAllViews()
        if (processingBean == null) return
        val layoutField = inflater.inflate(R.layout.table_row_material_field, table_material, false)
        table_material.addView(layoutField)
        val materialList = processingBean.materialList ?: return
        //原料
        materialList.forEach {
            materialFee = materialFee.addEx(it.totalPrice)
            val table = table(
                R.layout.table_row_material, it.materialName, it.materialNumber,
                it.materialWeight, it.materialPrice, it.totalPrice
            )
            table_material.addView(table)
        }
        //男工
        if (processingBean.maleWorker != 0f) {
            materialFee = materialFee.addEx(processingBean.maleTotalPayment)
            val table = table(
                R.layout.table_row_material, "男工", processingBean.maleWorker,
                null, processingBean.malePayment, processingBean.maleTotalPayment
            )
            table_material.addView(table)
        }
        //女工
        if (processingBean.womanPayment != 0f) {
            materialFee = materialFee.addEx(processingBean.womanTotalPayment)
            val table = table(
                R.layout.table_row_material, "女工", processingBean.womanWorker,
                null, processingBean.womanPayment, processingBean.womanTotalPayment
            )
            table_material.addView(table)
        }
        val totalFee = processingBean.materialNumber.orEmpty() * 10f
        val table = table(
            R.layout.table_row_material, "场地费", processingBean.materialNumber,
            null, null, totalFee
        )
        table_material.addView(table)

        materialFee = materialFee.addEx(totalFee)
        layout_material.tv_fee.contentText = "(${materialFee})"

    }


    fun product(productList: List<ProcessingProductBean>?) {
        table_product.removeAllViews()
        val layoutField = inflater.inflate(R.layout.table_row_material_field, table_product, false)
        if (productList == null) {
            return
        }
        table_product.addView(layoutField)
        var productFee = BigDecimal(0)
        productList.forEach {
            productFee = productFee.addEx(it.preTotalPrice)
            table_product.addView(
                table(
                    R.layout.table_row_material, it.productName,
                    it.productNumber, it.productWeight, it.prePrice, it.preTotalPrice
                )
            )
        }
        layout_product.tv_fee.contentText = "(${productFee})"
    }

    private fun table(resLayout: Int, name: String, number: Float?, weight: Float?, price: Float?, totalPrice: Float?): View {
        val layout = inflater.inflate(resLayout, null, false)
        layout.tv_name.contentText = name
        layout.tv_number.contentText = if (number == null || number == 0f) "" else number.toString()
        layout.tv_weight.contentText = if (weight == null || weight == 0f) "" else weight.toString()
        layout.tv_price.contentText = if (price == null || price == 0f) "" else price.toString()
        layout.tv_total_price.contentText = if (totalPrice == null || totalPrice == 0f) "" else totalPrice.toString()
        return layout
    }
}